package cbbx_sm.evaluation;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import cbbx_sm.decision_maker.DecisionMakerFactory;
import cbbx_sm.decision_maker.IDecisionMaker;
import cbbx_sm.parser.CameraData;
import cbbx_sm.parser.Parser;
import cbbx_sm.probabilistic_model.Cluster;
import cbbx_sm.probabilistic_model.IPredictor;
import cbbx_sm.probabilistic_model.ProbabilisticModel;
import cbbx_sm.probabilistic_model.SimpleHeuristicPredictor;
import cbbx_sm.probabilistic_model.SystemShortTermMemory;
import cbbx_sm.simulation.Scheduler;
import cbbx_sm.utils.Utility;

/*
 * We want to compare different decision maker (and we expect that
 * more semantics we introduce the better will be the results). So
 * we hope that the alfa-beta-cutoff decision maker has the best
 * performance.
 * 
 * We use a dumb predictor that is the Heuristic Prediictor, in order
 * to make faster tests.
 */
public class DifferentDMakers {
	public static void main(String[] args) throws IOException{
		CameraData data16 = Parser.parseFile("data/cameraData/2009052816.txt", "cam16");
		CameraData data17 = Parser.parseFile("data/cameraData/2009052817.txt", "cam17");
		CameraData data18 = Parser.parseFile("data/cameraData/2009052818.txt", "cam18");

		data16.setImageFile("data/images/16.jpg");
		data17.setImageFile("data/images/17.jpg");
		data18.setImageFile("data/images/18.jpg");
		
		List<Cluster> clusters16 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052816.txt", "cam16", data16);;
		List<Cluster> clusters17 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052817.txt", "cam17", data17);
		List<Cluster> clusters18 = Utility.getClusters("data/clusterData/Kruskal_150.0_2009052818.txt", "cam18", data18);;
		
		List<Cluster> fusedClusters = new Vector<Cluster>();
		
		fusedClusters.addAll(clusters16);
		fusedClusters.addAll(clusters17);
		fusedClusters.addAll(clusters18);
	
		List<CameraData> cameraData = new Vector<CameraData>();
		cameraData.add(data16);
		cameraData.add(data17);
		cameraData.add(data18);
		
		List<List<Cluster>> clusters = new Vector<List<Cluster>>();
		clusters.add(clusters16);
		clusters.add(clusters17);
		clusters.add(clusters18);
		
		System.out.println("NUMBER OF BBOX TO CATCH: " + (data16.getNumberOfFrameWithBoundingBoxes() +
							data17.getNumberOfFrameWithBoundingBoxes() + data18.getNumberOfFrameWithBoundingBoxes()));
		System.out.println("Starting calculating probs...");
//		ProbabilisticModel probModel = new ProbabilisticModel();
//		probModel.computeProbabilities(cameraData, fusedClusters, (18 - 13) * 60 * 60);
		ProbabilisticModel probModel = ProbabilisticModel.readUp("new_13-14-15-16-17-18_150Clust.ser");
//		probModel.setThreshold(0.06);
		System.out.println("Probability computed:");
		Scheduler scheduler;
																											// time_window_clsterold_framesold
		System.out.println();
		
		SystemShortTermMemory memory;
		IPredictor predictor;
		IDecisionMaker decisionMaker;
	
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/result_DiffDM_SFD.txt");

		for (double cut_off = 0.0; cut_off < 1.0; cut_off += 0.02){
			memory = new SystemShortTermMemory(1);
			predictor = new SimpleHeuristicPredictor(probModel, memory, fusedClusters);
			decisionMaker = DecisionMakerFactory.getSampleFromDistributionDecisionMaker(cut_off);
			
			scheduler.setDecisionMaker(decisionMaker);
			scheduler.setMemory(memory);
			scheduler.setPredictor(predictor);
			scheduler.sequentialRun();
			System.gc();
		}
		
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/result_DiffDM_MLC.txt");

		for (double cut_off = 0.0; cut_off < 1.0; cut_off += 0.02){
			memory = new SystemShortTermMemory(1);
			predictor = new SimpleHeuristicPredictor(probModel, memory, fusedClusters);
			decisionMaker = DecisionMakerFactory.getMostLikelyClusterDecisionMaker(cut_off);
			
			scheduler.setDecisionMaker(decisionMaker);
			scheduler.setMemory(memory);
			scheduler.setPredictor(predictor);
			scheduler.sequentialRun();
			System.gc();
		}
		
		scheduler = new Scheduler(cameraData, fusedClusters, clusters, probModel, "output/result_DiffDM_ABC.txt");

		for (double alfa = 0.0; alfa <= 1.0; alfa += 0.05)
			for (double cut_off = 0.0; cut_off < 1.0; cut_off += 0.02){
				memory = new SystemShortTermMemory(1);
				predictor = new SimpleHeuristicPredictor(probModel, memory, fusedClusters);
				decisionMaker = DecisionMakerFactory.getAlfaBetaDecisionMaker(alfa, 1.0 - alfa, cut_off);
				
				scheduler.setDecisionMaker(decisionMaker);
				scheduler.setMemory(memory);
				scheduler.setPredictor(predictor);
				scheduler.sequentialRun();
				System.gc();
			}
	}
}
